<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


/***
 * 标的投标，满标，还款自动任务model
 * 
 * */
class Tender_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
    
    //标的参数信息
	function arrays($key){
    	$data = array(
    		'borrow_type' 			=> array("1"=>"标的"),
    		'borrow_status' 		=> array("-1"=>"尚未发布", "0"=>"待发布", "1"=>"借款中", "3"=>"满额成功", 
    				"2"=>"审核未通过", "4"=>"满额未发布", "5"=>"满标审核未通过"),
    	
    		// TODO 新增宝付满标交易
    		'bor_trade_status' 		=> array(
    			"1"=>"宝付:申请满标交易成功"	, 
    			"2"=>"宝付:申请满标交易失败"	, 
    			"3"=>"宝付:满标交易成功", 	
    			"4"=>"宝付:满标交易失败"), 	
    		// ------ end
    		
    		// TODO 新增宝付投标交易
    		'bor_tend_status' 		=> array(
    			"0"=>"宝付:未申请"	, 
    			"1"=>"宝付:申请投标成功"	, 
    			"2"=>"宝付:申请投标失败"	,
    			"3"=>"宝付:投标成功", 		
    			"4"=>"宝付:投标失败"),
    	
    	 	// TODO 新增宝付还款交易
    	    'bor_repay_status' 		=> array(
    			"0"=>"宝付:未申请",	
    			"1"=>"宝付:申请还款成功",
    			"2"=>"宝付:申请还款失败",
    			"3"=>"宝付:还款成功",
    			"4"=>"宝付:还款失败"),
    		// ------ end
    		
    		'borrow_verify' 		=>array(0=>"未初检",1=>"初检通过",2=>"初检失败"),
    		'borrow_check' 			=>array(0=>"未复检",1=>"复检通过",2=>"复检失败"),
						
    	);

    }
    
	// 查询所有需要投标的标的
    function get_all_bow(){
         $_sql = " select borw.id,borw.user_id from rsh_borrow as borw
					where borw.fstatus = 3  and borw.bf_trade_status != 3 and account <=(
					select sum(tend.money) as sum_money from rsh_borrow_tender as tend  where tend.bf_trade_status != 3 and fstatus !=3 and borw.id = tend.borrow_id)";
    	$query = $this->db->query($_sql);
    	return $query->result_array();
    }
	
	// 查询标的ID所对应的投资者信息
    function get_bow_tender($bow_id){
    	$_sql = " select id,user_id,money,account,bf_trade_status,fstatus from rsh_borrow_tender where bf_trade_status != 3 and fstatus !=3  and borrow_id = {$bow_id}";
    	$query = $this->db->query($_sql);
    	return $query->result_array();
    }
    
    
    // 查询所有需要满标的标的
    function get_all_full_bow(){
        $_sql = " select borw.id,borw.user_id from rsh_borrow as borw
					where borw.fstatus = 3 and borw.bf_trade_status != 3 and account <=(
					select sum(tend.money) as sum_money from rsh_borrow_tender as tend  where tend.bf_trade_status = 3 and fstatus !=3 and borw.id = tend.borrow_id)";
    	$query = $this->db->query($_sql);
    	return $query->result_array();
    }
    
	// 获取标的用户实际投标总额
	function getFullMoney($borrow_id,$fstatus){
		// 所有投标成功状态 以 宝付反馈结果为准
		$_sql = " select sum(account) as sum_money from rsh_borrow_tender where borrow_id = {$borrow_id} and bf_trade_status = 3 and fstatus !=3 ";
		$query = $this->db->query($_sql);
		if($query->num_rows() > 0){
			return $query->row()->sum_money;
		}else{
			return 0 ;
		}
	}
	
	// 更新
	function update($update){
		$query = $this->db->query($update);
		if($query->num_rows() > 0){
			return 1;
		}else{
			return 0 ;
		}
	}
	
	
	
	// 20150619 修改：计算投资积分
	function cul_score($bow_id){
		$this->load->model('User_model', 'user');
		$this->load->model('Borrow_model', 'borrow');
		$this->load->model('Invest_model', 'invest');
		$_sql = " select user_id,sum(money)  as sum_money from rsh_borrow_tender  where  borrow_id = {$bow_id} 
			and fstatus = 1  group by user_id ";
		$query = $this->db->query($_sql);
		$op_falg = 1;//增加
		$op_user = 1;//系统员
		$arrs = $query->result_array();
		foreach ($arrs  as $arr) {
			$this->db->trans_strict(FALSE);
			$this->db->trans_begin();
			$borw = $this->borrow->get_borrow_obj($bow_id);
			$user = $this->user->get_by_uid($arr['user_id']);
			$borw_day = 0 ;
			if($borw['is_day'] == 1){
				$borw_day = $borw['time_limit_day'];
			}else{
				$borw_day = $borw['time_limit'] * 30;
			}
			$_old_invest 	= $user['invest_score'];
			// 20150618 投资金额 / 360 * 投标天数 * 标的年化利率 
			$_add_invest 	= round(($arr['sum_money']/360) * $borw_day * (($borw['apr']+$borw['funds']) /100 )) ;
			$_new_invest 	= $_old_invest+$_add_invest;
			$id = $this->invest->get_rank_id($_new_invest);
			if($id == false){
				$id = 0;
			}
			$_update_user = " update rsh_user set invest_score = {$_new_invest} , invest_rank_id = {$id} where user_id = {$arr['user_id']}";
			$query_user = $this->db->query($_update_user);
			$add_time = time();
			$_insert_log   = " insert into rsh_invest_log (user_id,op,invest_score,op_user,add_time) values(
				{$arr['user_id']},{$op_falg},{$_add_invest},{$op_user},{$add_time}
			) ";
			$query_invest = $this->db->query($_insert_log);
			if($this->db->trans_status() === TRUE) {
        		$this->db->trans_commit();
    		} else {
      			$this->db->trans_rollback();
   			}
		}
	}
	
	
	        
    // 专属标的匹配规则：CAPTIVE_(标的号) == 专属人
    function getCaptiveUser($key){
    	$captive = Array( 15=>15316, 16=>15316, 1=>15311 ,);
    	if(array_key_exists($key, $captive)){
    		return $captive[$key];
			}
    }
    
	
	//TODO 数据库资金日志迁移，每日凌晨 00:40 分执行
	function copyData(){
		$start_time =   strtotime(date("Y-m-d",time())) - 3600 * 24 ;	 		 // 昨日 00:00:00
		$end_time 	= 	strtotime(date("Y-m-d",time())) - 1;		 			 // 昨日 23:59:59
		for ($i = 0 ;$i<10;$i++){
			$_sql = " INSERT INTO rsh_account_log( user_id,ftype,total,money,use_money,no_use_money,collection,to_user,remark,add_time,addip,op )
					  SELECT user_id,ftype,total,money,use_money,no_use_money,collection,to_user,remark,add_time,addip,op
					  FROM rsh_account_log_{$i}
					  WHERE add_time <= {$end_time}  AND add_time >= {$start_time} ";
			
			$query = $this->db->query($_sql);
		}
		
	}
	
	//TODO 密码错误次数每日更新为0，每日凌晨 00:30 分执行
	function upError(){		
		
		$_sql = " update rsh_user set error_num = 0 where error_num != 0 ";
		$query = $this->db->query($_sql);
	}
	
}
